﻿/*Hdip Project 2013-14, Student Name:- Chris Pembroke,  Student Number:- x00115839*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Exercise1Project.Models;  // added name space
using Exercise1Project.Models.Validation;

namespace Exercise1Project.Controllers
{
    public class BikeServiceController : Controller
    {
       // private BicycleRepairEntities1 _bs = new BicycleRepairEntities1();
       // private IBikeServiceRepository _repository; // instance of bikeService repository interface


        private IBikeServiceBusinessLogic _service;

        public BikeServiceController() // non parameterised constructor 
        {
            _service = new BikeServiceBusinessLogic(new ModelStateWrapper(this.ModelState));
        }

        public BikeServiceController(IBikeServiceBusinessLogic service)  // parameterised constructor
        {
            _service = service;
        }

        
        // GET: /BikeService/list of bike services for a given bike
        [HttpGet]
        public ActionResult ServiceList(int id )
        {
            var bikeServiceList = _service.GetBikeServiceByBikeID(id);
            
            if (bikeServiceList == null)  // there is a list of bikes for this customer
            {
                return HttpNotFound();
            }
            return View("ServiceList", bikeServiceList);
        }

        //GET: /bikeservice /create service-quote
        [HttpGet]
        public ActionResult Create(int id)
        {           
            if (id == 0)
            {
                TempData["QuoteOrService"] = "Quote";
            }
            else
            {
                TempData["QuoteOrService"] = "Service";
            }

            ViewBag.Items = new SelectList(BikeService.ItemsOptions);
            ViewBag.Actions = new SelectList(BikeService.ActionsOptions);
            ViewBag.ServiceItems = new SelectList(BikeService.ServiceItemsList);
            
            return View("Create");
        }

        //POST: /bikeservice/createservice - quote
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "ServiceId")]BikeService bikeServiceToAdd)                       
        {      
                    ViewBag.Items = new SelectList(BikeService.ItemsOptions);
                    ViewBag.Actions = new SelectList(BikeService.ActionsOptions);
                    ViewBag.ServiceItems = new SelectList(BikeService.ServiceItemsList);
            
               if(_service.AddBikeService(bikeServiceToAdd))   // working
                     return RedirectToAction("ServiceList", new { id = bikeServiceToAdd.BikeID });

               ViewBag.CreateError = "Must Select Items for Service!";  // if no items selected for service 
               return View("Create", bikeServiceToAdd );  // working
        }

        //GET: /Bikeservice/Edit
        [HttpGet]
        public ActionResult Edit(int id)
        {
            var bikeServiceToUpdate = _service.GetBikeServiceByServiceId(id);

            if (bikeServiceToUpdate == null)
            {
                return HttpNotFound();
            }

            ViewBag.Items = new SelectList(BikeService.ItemsOptions);
            ViewBag.Actions = new SelectList(BikeService.ActionsOptions);
            ViewBag.ServiceItems = new SelectList(BikeService.ServiceItemsList);       // empty list
            List<SelectListItem> statusBikeInShop = new List<SelectListItem>()
                    {
                      new SelectListItem { Value = "Not In Shop", Text = "Not In Shop"}, 
                      new SelectListItem { Value = "In Shop", Text = "In Shop"},
                      new SelectListItem {Value = "Collected", Text = "Collected"}
                    };
            var itemToBeSelected1 = statusBikeInShop.FirstOrDefault(x => x.Value == bikeServiceToUpdate.StatusBikeInShop);
            if (itemToBeSelected1 != null)
                itemToBeSelected1.Selected = true;

            ViewBag.StatusBikeInShop = statusBikeInShop; // new SelectList(BikeService.BikeInShopOptions);

            List<SelectListItem> customerStatusService = new List<SelectListItem>() 
                                   {
                                      new SelectListItem { Value = "Quote", Text = "Quote"},
                                      new SelectListItem { Value = "Booked", Text = "Booked"},
                                      new SelectListItem { Value = "Cancelled", Text = "Cancelled"},
                                   };
            var itemToBeSelected2 = customerStatusService.FirstOrDefault(x => x.Value == bikeServiceToUpdate.StatusService);
            if (itemToBeSelected2 != null)
                itemToBeSelected2.Selected = true;

            ViewBag.StatusService = customerStatusService;   // new SelectList(BikeService.admin_ServiceStatusOptions); 

            return View("Edit", bikeServiceToUpdate);
        }

        //POST:  / bikeService/Edit 
        [HttpPost]
        public ActionResult Edit(BikeService bikeServiceToUpdate)
        {
                if (ModelState.IsValid)
                {
                    _service.UpdateBikeService(bikeServiceToUpdate);
                    return RedirectToAction("ServiceList", new { id = bikeServiceToUpdate.BikeID });
                }

                return View("Edit", bikeServiceToUpdate);

        } // close of actionresult postEdit

        //GET: /bikeservice/delete/
        [HttpGet]
        public ActionResult Delete(int id)
        {
            var bikeServiceToDelete = _service.GetBikeServiceByServiceId(id);
            if (bikeServiceToDelete == null)
            {
                return HttpNotFound();
            }
            return View("Delete", bikeServiceToDelete);
        }

        //POST: /bike/delete
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            BikeService bikeServiceToDelete = _service.GetBikeServiceByServiceId(id);

            if (_service.RemoveBikeService(bikeServiceToDelete.ServiceID))
                return RedirectToAction("ServiceList", new { id = bikeServiceToDelete.BikeID });

            return View("Delete", new { id = bikeServiceToDelete.ServiceID });
        }

        public ActionResult Details(int id)
        {
            BikeService bikeServiceDetail = _service.GetBikeServiceByServiceId(id);

            if (bikeServiceDetail == null)
            {
                return HttpNotFound();
            }
            return View("Details", bikeServiceDetail);
        }

        public ActionResult Paypal(int id)
        {
            BikeService bikeServiceDetail = _service.GetBikeServiceByServiceId(id);

            if (bikeServiceDetail == null)
            {
                return HttpNotFound();
            }
            return View("Paypal", bikeServiceDetail);
        }

        public ActionResult BikeList(string id)  // go back from list of services to customers bike list to action Index in Bike controller
        {
            return RedirectToAction("Index", "Bike", new { id = id });
        }


        //----------------------------------------ADMIN SECTION --------------------------------------------------

        //GET: /adminsearches/...administration first view for selecting searches based on service status and/or bike in shop status
        [HttpGet]
        public ActionResult AdminSearches()
        {
            BikeService model = new BikeService();  // empty model 
            return View(model);  // empty model sent and bound to search view
        }

        //Post: /adminsearches/...administration first view for selecting searches based on service status and/or bike in shop status
        [HttpPost]
        public ActionResult AdminSearches(BikeService model)
        {
           
            try
            {
                var adminSearchResult = _service.returnAdminSearchQuery(model);
                return View("AdminSearchResults", adminSearchResult);

                /*
                if (model.BikeServiceStatus == "All")  // search all bike service statuses
                {
                    if (model.BikeInStoreStatus == "All")  // use all bike in store statuses
                    {
                        var query1 = _service.GetAllBikeServices();
                        return View("AdminSearchResults", query1);
                    }
                    else                            // only use selected bike in store status
                    {
                        var query2 = _service.GetAllBikeServicesByBikeInShopStatus(model.BikeInStoreStatus);
                        return View("AdminSearchResults", query2);
                    }
                }
                else // search only selected bike service status
                {
                    if (model.BikeInStoreStatus == "All")  // use all bike in store statuses
                    {
                        var query3 = _service.GetAllBikeServicesByServiceStatus(model.BikeServiceStatus);
                        return View("AdminSearchResults", query3);
                    }
                    else                            // only use selected bike in store status
                    {
                        var query4 = _service.GetAllBikeServicesByInShopAndServiceStatus(model.BikeServiceStatus, model.BikeInStoreStatus);
                        return View("AdminSearchResults", query4);
                    }

                }
                 */
            }
            catch 
            {
                ViewBag.Error = "No Results Found!";
                return View();
            } 
             
        }

        //GET: /Bikeservice/AdminEdit
        [HttpGet]
        public ActionResult AdminEdit(int id)
        {
            BikeService  bikeServiceToUpdate = _service.GetBikeServiceByServiceId(id);

            if(bikeServiceToUpdate == null)
            {
                return HttpNotFound();
            }
            else
            {   
                    ViewBag.Items = new SelectList(BikeService.ItemsOptions);
                    ViewBag.Actions = new SelectList(BikeService.ActionsOptions);
                    ViewBag.ServiceItems = new SelectList(BikeService.ServiceItemsList);

                    List<SelectListItem> statusBikeInShop = new List<SelectListItem>()
                    {
                      new SelectListItem { Value = "Not In Shop", Text = "Not In Shop"}, 
                      new SelectListItem { Value = "In Shop", Text = "In Shop"},
                      new SelectListItem {Value = "Collected", Text = "Collected"}
                    };
                    var itemToBeSelected1 = statusBikeInShop.FirstOrDefault(x => x.Value == bikeServiceToUpdate.StatusBikeInShop);
                    if (itemToBeSelected1 != null)
                        itemToBeSelected1.Selected = true;

                    ViewBag.StatusBikeInShop = statusBikeInShop; // new SelectList(BikeService.BikeInShopOptions);
                
                    List<SelectListItem> adminStatusService = new List<SelectListItem>() 
                                   {
                                      new SelectListItem { Value = "Quote", Text = "Quote"},
                                      new SelectListItem { Value = "Booked", Text = "Booked"},
                                      new SelectListItem { Value = "Cancelled", Text = "Cancelled"},
                                      new SelectListItem { Value = "Done", Text = "Done"},
                                      new SelectListItem { Value = "Paid", Text = "Paid"}
                                   };
                    var itemToBeSelected2 = adminStatusService.FirstOrDefault(x => x.Value == bikeServiceToUpdate.StatusService);
                    if (itemToBeSelected2 != null)
                        itemToBeSelected2.Selected = true;

                    ViewBag.StatusService = adminStatusService;   // new SelectList(BikeService.admin_ServiceStatusOptions); 
  
                    return View("AdminEdit", bikeServiceToUpdate);
            }
        }

        //POST:  / bikeService/AdminEdit 
        [HttpPost]
        public ActionResult AdminEdit(BikeService bikeservice)
        {
            if (ModelState.IsValid)
            {
                if(_service.UpdateBikeService(bikeservice))
                return RedirectToAction("AdminDetails", new { id = bikeservice.ServiceID });
            }
            return View("AdminEdit", bikeservice);

        } // close of actionresult postEdit

        [HttpGet]
        public ActionResult AdminDetails(int id)
        {
            var bikeServiceDetail = _service.GetBikeServiceByServiceId(id);
            if (bikeServiceDetail == null)
            {
                return HttpNotFound();
            }
            return View("AdminDetails", bikeServiceDetail);
        }

        public ActionResult EmailCustomer(int id)  // goes over to the EamilCustomer Controller and to CreateEmail Action there
        {
            return RedirectToAction("CreateEmail", "EmailCustomer", new { id = id });
        }



	}// close of class  bikeservicecontroller

} // close of namespace exercise1projects.controllers
